<template>
  <div class="box">
    <div class="left">
      <div class="title">
        <p>Weblog博客登录</p>
        <img
          src="http://47.104.167.34:9110/assets/login-logo-57e339e7.png"
          alt="登录logo"
        />
      </div>
    </div>
    <div class="right">
      <div class="login">
        <h2>欢迎回来</h2>
        <span>------------</span>
        <span>账号密码登录</span>
        <span>------------</span>
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="ruleForm.username"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post("/api/login", this.ruleForm)
            .then((res) => {
              if (res.data.success) {
                localStorage.setItem("token", res.data.data.token);
                this.$message.success("登录成功");
                this.$router.push("/after");
              } else {
                this.$message.error(res.data.msg || "登录失败");
              }
            })
            .catch((err) => {
              console.error("登录请求失败：", err);
              this.$message.error("网络异常或服务器错误，请稍后再试");
            });
        } else {
          console.log("表单验证失败！");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
/* 样式保持不变 */
.box {
  margin: 0 auto;
  display: flex;
}
.left {
  width: 70%;
  height: 100vh;
  background-color: #001428;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  color: #fff;
}
.title {
  width: 100%;
  font-size: 40px;
  font-weight: bold;
  text-align: center;
}
.title img {
  width: 45%;
  height: 45%;
}
.right {
  width: 30%;
  height: 100vh;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}
.login {
  text-align: center;
}
.login span {
  color: #9ca3af;
  font-size: 12px;
}
.demo-ruleForm {
  margin-bottom: 70px;
}
</style>